package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.secondActivity.ActivityVerification;

public class SmsBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 获取接收到的电话和消息
        String phoneNumber = intent.getStringExtra("phoneNumber");
        String message = intent.getStringExtra("message");

        Log.d("TEST//", "Received phoneNumber: " + phoneNumber + " message: " + message);

        // 存储收到的短信信息到 SharedPreferences
        SharedPreferences sharedPreferences = context.getSharedPreferences("SmsData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("phoneNumber", phoneNumber);
        editor.putString("message", message);

        // 获取已接收的短信数量，并更新
        int messageCount = sharedPreferences.getInt("messagenum", 0);
        messageCount++;  // 每接收到一条短信，增加数量

        // 更新 SharedPreferences 中的短信计数
        editor.putInt("messagenum", messageCount);
        editor.apply();

        // 显示接收到的消息数量
        Toast.makeText(context, "收到短信！号码: " + phoneNumber + " 内容: " + message, Toast.LENGTH_LONG).show();

        // 更新 Activity 中的 TextView
        if (context instanceof ActivityVerification) {
            ActivityVerification activityVerification = (ActivityVerification) context;
            activityVerification.updateMailCount(messageCount);  // 调用 updateMailCount 更新 UI
        }
    }
}
